package com.gateway;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.serializer.impl.SaSerializerTemplateForJdkUseBase64;
import jakarta.annotation.PostConstruct;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApp {
    public static void main(String[] args) {
        org.springframework.boot.SpringApplication.run(GatewayApp.class, args);
        System.out.println("Gateway application started successfully!");
    }
    @PostConstruct
    public void rewriteComponent() {
        SaManager.setSaSerializerTemplate(new SaSerializerTemplateForJdkUseBase64());
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // Key序列化为String
        template.setKeySerializer(new StringRedisSerializer());
        // Value序列化为JSON（需引入Jackson）
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        // Hash类型序列化
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        return template;
    }
}
